package com.xyzwps.paimon.furina.common.value

import com.xyzwps.paimon.furina.common.AppErrorCode
import com.xyzwps.paimon.furina.common.AppException
import io.swagger.v3.oas.annotations.media.Schema
import jodd.util.RandomString

@Schema(type = "string", example = "121212", description = "验证码")
data class Vcode(override val value: String) : StringValue {
    init {
        if (!VCODE_PATTERN.matcher(value).matches()) {
            throw AppException(AppErrorCode.VCODE_FORMAT_INVALID, "验证码格式不正确")
        }
    }


    companion object {
        private val random = RandomString()

        fun randomVcode() = Vcode(random.randomNumeric(6))
    }
}
